/*******************************************************************************
 * Copyright (c) 2014-6-26 @author <a href="mailto:iffiff1@hotmail.com">Tyler Chen</a>.
 * All rights reserved.
 *
 * Contributors:
 *     <a href="mailto:iffiff1@hotmail.com">Tyler Chen</a> - initial API and implementation
 ******************************************************************************/
package com.foreveross.project.projectonline.jfinal.plugin;

import java.util.HashSet;
import java.util.Set;

import org.iff.infra.util.Assert;

import redis.clients.jedis.JedisPoolConfig;
import redis.clients.jedis.JedisSentinelPool;

import com.jfinal.plugin.IPlugin;

/**
 * @author <a href="mailto:iffiff1@hotmail.com">Tyler Chen</a> 
 * @since 2014-6-26
 */
public class JedisSentinelPlugin implements IPlugin {

	public static String JEDIS_PROPERTIES = "jedis.properties";
	String sentinels = null;
	String masterName = null;
	String password = null;
	JedisSentinelPool pool = null;
	int timeout = 2000;

	public JedisSentinelPlugin() {
	}

	public JedisSentinelPlugin(String sentinels, String masterName,
			String password) {
		this.sentinels = sentinels;
		this.masterName = masterName;
		this.password = password;
	}

	public void init() {
		{//testing
			sentinels = sentinels != null ? sentinels : ResourceKit.getValue(
					JEDIS_PROPERTIES, "jedis.sentinels");
			masterName = masterName != null ? masterName : ResourceKit
					.getValue(JEDIS_PROPERTIES, "jedis.masterName");
			password = password != null ? password : ResourceKit.getValue(
					JEDIS_PROPERTIES, "jedis.password");
			try {
				String to = ResourceKit.getValue(JEDIS_PROPERTIES,
						"jedis.timeout");
				timeout = Integer.valueOf(to);
			} catch (Exception e) {
			}
			Assert.notEmpty(sentinels);
			Assert.notEmpty(masterName);
			Assert.notEmpty(password);
		}
		Set<String> sentinelSet = new HashSet<String>();
		{
			String[] splits = sentinels.split(",");
			for (String sentinel : splits) {
				sentinelSet.add(sentinel);
			}
		}
		JedisPoolConfig config = new JedisPoolConfig();
		{
			setJedisPoolConfig(config);
		}
		pool = new JedisSentinelPool(masterName, sentinelSet, config, timeout,
				password);
		//public JedisSentinelPool(String masterName, Set<String> sentinels, GenericObjectPoolConfig poolConfig, int timeout, String password, int database)
		//Jedis jedis = pool.getResource();
		//jedis.set("jedis", "jedis");
		//pool.returnResource(jedis);
		JedisSentinelKit.init(pool);
	}

	public boolean start() {
		init();
		return true;
	}

	public boolean stop() {
		try {
			pool.destroy();
			JedisSentinelKit.init(null);
		} catch (Exception e) {
		}
		return true;
	}

	protected void setJedisPoolConfig(JedisPoolConfig config) {
		try {
			config.setMaxTotal(Integer.valueOf(ResourceKit.getValue(
					JEDIS_PROPERTIES, "jedis.maxTotal")));
		} catch (Exception e) {
		}
		try {
			config.setMaxIdle(Integer.valueOf(ResourceKit.getValue(
					JEDIS_PROPERTIES, "jedis.maxIdle")));
		} catch (Exception e) {
		}
		try {
			config.setMinIdle(Integer.valueOf(ResourceKit.getValue(
					JEDIS_PROPERTIES, "jedis.minIdle")));
		} catch (Exception e) {
		}
		try {
			config.setLifo(Boolean.valueOf(ResourceKit.getValue(
					JEDIS_PROPERTIES, "jedis.lifo")));
		} catch (Exception e) {
		}
		try {
			config.setMaxWaitMillis(Long.valueOf(ResourceKit.getValue(
					JEDIS_PROPERTIES, "jedis.maxWaitMillis")));
		} catch (Exception e) {
		}
		try {
			config.setMinEvictableIdleTimeMillis(Long.valueOf(ResourceKit
					.getValue(JEDIS_PROPERTIES,
							"jedis.minEvictableIdleTimeMillis")));
		} catch (Exception e) {
		}
		try {
			config.setSoftMinEvictableIdleTimeMillis(Long.valueOf(ResourceKit
					.getValue(JEDIS_PROPERTIES,
							"jedis.softMinEvictableIdleTimeMillis")));
		} catch (Exception e) {
		}
		try {
			config.setNumTestsPerEvictionRun(Integer
					.valueOf(ResourceKit.getValue(JEDIS_PROPERTIES,
							"jedis.numTestsPerEvictionRun")));
		} catch (Exception e) {
		}
		try {
			String value = ResourceKit.getValue(JEDIS_PROPERTIES,
					"jedis.evictionPolicyClassName");
			if (value != null && value.length() > 0) {
				config.setEvictionPolicyClassName(value);
			}
		} catch (Exception e) {
		}
		try {
			config.setTestOnBorrow(Boolean.valueOf(ResourceKit.getValue(
					JEDIS_PROPERTIES, "jedis.testOnBorrow")));
		} catch (Exception e) {
		}
		try {
			config.setTestOnReturn(Boolean.valueOf(ResourceKit.getValue(
					JEDIS_PROPERTIES, "jedis.testOnReturn")));
		} catch (Exception e) {
		}
		try {
			config.setTestWhileIdle(Boolean.valueOf(ResourceKit.getValue(
					JEDIS_PROPERTIES, "jedis.testWhileIdle")));
		} catch (Exception e) {
		}
		try {
			config.setTimeBetweenEvictionRunsMillis(Long.valueOf(ResourceKit
					.getValue(JEDIS_PROPERTIES,
							"jedis.timeBetweenEvictionRunsMillis")));
		} catch (Exception e) {
		}
		try {
			config.setBlockWhenExhausted(Boolean.valueOf(ResourceKit.getValue(
					JEDIS_PROPERTIES, "jedis.blockWhenExhausted")));
		} catch (Exception e) {
		}
	}
}
